import * as api from '../api/api';
import request from '../utils/request';
import { pageSize } from '../utils/common';
import store from './store';
export default {
    namespace: 'article',
    state: {

        article: {},
        articles: { totalCount: 0, items: [] },
        articleSearchs: {  keywords: '',articletType:'',pageIndex: 1, pageSize },
    },
    effects: {

        /**
         * 获取文章信息
         * @param param0 
         * @param param1 
        */
        *getArticles({ payload }, { call, put }) {
            const {keywords,articletType, pageIndex, pageSize } = payload;
            const
                formKeywords = keywords,
                formArticleType = articletType,
                formSkipCount = (pageIndex - 1) * pageSize,
                formMaxResultCount = pageSize;
            const articles =
                yield request(api.ManagearticlesManageApiFetchParamCreactor.managearticlesManageGetAllArticlesAsync({
                    formKeywords,formArticleType, formSkipCount, formMaxResultCount
                }))
            yield put({ type: 'save', payload: { articles, articleSearchs: {  keywords,articletType,pageIndex, pageSize, } } });
        },

        /**
         * 保存文章信息
         * @param param0 
         * @param param1 
         */

        *saveArticles({ payload }, { call, put, select }) {
            const { form, callback } = payload;
            const { articleSearchs } = yield select(({ article }) => article);
            yield request(api.ManagearticlesManageApiFetchParamCreactor.managearticlesManageSaveArticlesAsync(
                { form }
            ));
            yield put({ type: 'getArticles', payload: { ...articleSearchs } });
            callback && callback(true);
        },
        
         /**
         * 删除文章
         * @param param0 
         * @param param1 
         */
        *removeArticles({ payload }, { call, put }) {
            const { form, callback } = payload;
            yield request(api.ManagearticlesManageApiFetchParamCreactor.managearticlesManageDeleteArticlesAsync(
                { form }
            ));
            callback && callback(true);
        },
    },
    reducers: {
        save(state, action) {
            return { ...state, ...action.payload };
        },
    },
    subscriptions: {
        setup({ dispatch, history }) {
                return history.listen(({ pathname, search }) => {
                    switch (pathname) {
                        case '/article/articles':
                            dispatch({ type: 'getArticles', payload: { pageIndex: 1, pageSize } });
                            break;
                    }
                });
        },
    },
};